/*
 * JQuery zTree core 3.3
 * http://zTree.me/
 *
 * Copyright (c) 2010 Hunter.z
 *
 * Licensed same as jquery - MIT License
 * http://www.opensource.org/licenses/mit-license.php
 *
 * email: hunter.z@263.net
 * Date: 2012-07-16
 */
(function($) {
	var settings = {
		hiddenName : "orgId",
		hiddenType:"orgType",
		showName : "orgName",
		typeofshow : "radio",// check,radio
		currentOrgInfo : {}// 记录当前机构下的机构标识orgId : "",orgType : ""//1：平台   有值传入值，否则以当前登录信心为主 ；managetype:0向下兼容，默认或者1 当前管理员管理角色
	}
	var radioSettingDefault = {
		isSimpleData : true, // 数据是否采用简单 Array 格式，默认false
		treeNodeKey : "id", // 在isSimpleData格式下，当前节点id属性
		treeNodeParentKey : "pId", // 在isSimpleData格式下，当前节点的父节点id属性
		showLine : true, // 是否显示节点间的连线
		check : {
			enable : true,
			chkStyle : "radio",
			radioType : "all"
		},
		data : {
			key : {
				name : "name"
			},
			simpleData : {
				enable : true
			}
		},
		callback : null
	};
	var checkSettingDefault = {
		isSimpleData : true, // 数据是否采用简单 Array 格式，默认false
		treeNodeKey : "id", // 在isSimpleData格式下，当前节点id属性
		treeNodeParentKey : "pId", // 在isSimpleData格式下，当前节点的父节点id属性
		showLine : true, // 是否显示节点间的连线
		check : {
			enable : true,
			chkboxType : {
				"Y" : "",
				"N" : ""
			}
		},
		data : {
			key : {
				name : "name"
			},
			simpleData : {
				enable : true
			}
		}
	};
	function transdatas(orgData) {
		var treeNodes = [];
		if (orgData && orgData.length > 0) {
			for (var i = 0; i < orgData.length; i++) {
				if (orgData[i]) {
					var treeNode = {};
					treeNode.id = orgData[i].orgId;
					treeNode.name = orgData[i].orgName;
					treeNode.pId = orgData[i].parentOrgId;
					treeNode.orgType = orgData[i].orgType;
					treeNode.orgStatus = orgData[i].orgStatus;
					treeNode.open = true;
					treeNodes.push(treeNode);
				}
			}
		}
		return treeNodes;
	}
	function showAll(status, id, zTree) {
		if (status == "0") {//关闭
		} else if(status == "1"){//确定
			var a = zTree.getCheckedNodes("tree");// 此数据为选中的数据
			var treeValue = "";
			var showTree = "";
			var treeTypeValue = "";
			for ( var i in a) {
				if (treeValue) {
					treeValue += "," + a[i].id;
				} else {
					treeValue = a[i].id;
				}
				if (treeTypeValue) {
					treeTypeValue += "," + a[i].orgType;
				} else {
					treeTypeValue = a[i].orgType;
				}
				if (showTree) {
					showTree += "," + a[i].name;
				} else {
					showTree = a[i].name;
				}
			}
			$("#" + id + "_treeValue").val(treeValue);
			$("#" + id + "_treeTypeValue").val("");//多选没有数据
			$("#" + id + "_showTree").val(showTree);
			$("#" + id + "_showTree").val(showTree);
		}else{//清空
			$("#" + id + "_treeValue").val("");
			$("#" + id + "_treeTypeValue").val("");
			$("#" + id + "_showTree").val("");
			$("#" + id + "_showOrgAll").val("");
			zTree.checkAllNodes(false);//多选的删除
		}
		$("#" + id + "_treeblock").hide();
	}
	
	$.fn.orgtree = function(opt) {
		var id = $(this).attr("id");
		var setting = {};
		var treeSetting = {};
		if (opt) {
			$.extend(setting, settings, opt);
		}
		var orgshowhtml = "<input type='hidden' name='" + setting.hiddenName+ "' id='" + id + "_treeValue'/>"
				+"<input type='hidden' name='" + setting.hiddenType	+ "' id='" + id + "_treeTypeValue' />"
				+"<input type='hidden' id='" + id + "_showOrgAll' />"
				+"<input type='text' class='form-control' name='"+ setting.showName + "' id='" + id + "_showTree'/>"
				+"<div id='" + id + "_treeblock' class='treeblock'>";

		if (setting.typeofshow == "radio") {//单选
			var radioSetting = $.extend(true,{}, radioSettingDefault);
			orgshowhtml += "<div class='margin_top'><div id='" + id
					+ "_tree' class='ztree'/></div>"
					+ "<div class='hoverbottom'>"
					+ "<div class='button2' data-init='2'>清除</div><div class='button2' data-init='0'>关闭</div>"
					+ "</div>";
			radioSetting.callback = {
				onCheck : function(event, treeId, treeNode) {
					var _obj ={};//将机构数据封装
						_obj.orgId= treeNode.id;
						_obj.orgType= treeNode.orgType;
						_obj.orgName= treeNode.name;
						_obj.orgStatus = treeNode.orgStatus;
					$("#" + id + "_treeValue").val(_obj.orgId);
					$("#" + id + "_treeTypeValue").val(_obj.orgType);
					$("#" + id + "_showTree").val(_obj.orgName);
					$("#" + id + "_showOrgAll").val(JSON.stringify(_obj));
					$("#" + id + "_treeblock").hide();
				}
			}
			treeSetting = radioSetting;
		} else {//多选
			orgshowhtml += "<div class='margin_top'><div id='"
					+ id
					+ "_tree' class='ztree'/></div>"
					+ "<div class='hoverbottom'>"
					+ "<div class='button2' data-init='1'>确定</div><div class='button2' data-init='2'>清除</div><div class='button2' data-init='0'>关闭</div>"
					+ "</div>";
			treeSetting = $.extend(true,{}, checkSettingDefault);
		}
		orgshowhtml += "</div></div>";
		$(this).empty();
		$(this).html(orgshowhtml);
		var zTree = $("#" + id + "_tree");
		console.log("--------------------------------------");
		console.log(setting.currentOrgInfo);
		jsonAjax("get", "/org/orgCommonInfo/downlists", setting.currentOrgInfo,
				"json", function(result) {
					if (result.code == 0) {
						var treeNodes = transdatas(result.rs.data);
						zTree = $.fn.zTree.init(zTree, treeSetting, treeNodes);
						$("#" + id + "_showTree").bind("focus", function() {
							$("#" + id + "_treeblock").show();
						});
						$("#" + id +  "_treeblock .hoverbottom .button2").unbind('click').bind('click', function() {
							showAll($(this).attr("data-init"), id, zTree);
						})
					} else {
						console.log("暂无机构信息");
					}
				});
	}
	$.fn.orgtree.setHiddenNameValue = function(id,value){
		$("#" + id + "_treeValue").val(value);
	}
	$.fn.orgtree.setHiddenTypeValue = function(id,value){
		$("#" + id + "_treeTypeValue").val(value);
	}
	$.fn.orgtree.setshowNameValue = function(id,value){
		$("#" + id + "_showTree").val(value);
	}
	$.fn.orgtree.getHiddenNameValue = function(id){
		return $("#" + id + "_treeValue").val();
	}
	$.fn.orgtree.getHiddenTypeValue = function(id){
		return $("#" + id + "_treeTypeValue").val();
	}
	$.fn.orgtree.getshowNameValue = function(id){
		return $("#" + id + "_showTree").val();
	}
	//获取当前选中的所有的数据内容（仅限单选使用）
	$.fn.orgtree.getCurrentValue = function(id){
		return $("#" + id + "_showOrgAll").val();
	}
})(jQuery);
